<?php
class Cron extends Controller
{
    var $admin_email = 'pawan.joshi@unhabitat.org.np';
	function Cron()
	{
		parent::Controller();	
	}
	
	function index()
	{
		//do nothing
	}
	
	function email_notification()
	{
                $this->load->helper('url');
		$this->load->model('borrower_model');
		$to_s = $this->borrower_model->get_notifications_to_s();
               //debug_array($to_s);
		if(count($to_s) > 0)
		{
                    $sentEmailList = array();
                    foreach($to_s as $to)
			{/******************** for email **********************/
                            $this->email_format($to->email,  $this->admin_email, $to->first_name, $to->last_name);
                            $sentEmailList[]=ucfirst($to->first_name)." ".ucfirst($to->last_name)."($to->email)";
                            $this->update_borrower_quantity_status($to->borrower_qty_id);
			/******************************************************/
			}
		}
                $this->session->set_flashdata('success_message', 'Email notification successfully sent:<br/>'.implode(', ',$sentEmailList));
                redirect(site_url('home'));
	}
        function email_single_notification(){
            $email_from = $this->admin_email;
            $email_to = $this->input->post('email');
            $first_name = $this->input->post('first_name');
            $last_name = $this->input->post('last_name');
            $borrower_qty_id = $this->input->post('borrower_qty_id');
            $this->email_format($email_to, $email_from, $first_name,$last_name);
            $this->update_borrower_quantity_status($borrower_qty_id);
            echo "Email notification successfully sent to:".ucfirst($first_name)." ".ucfirst($last_name)."($email_to)";;
        }

        function email_format($email_to,$email_from, $fname, $lname){
            /******************** for email **********************/
            $email_to = rtrim($email_to);
            $email_from = rtrim($email_from);
            $message = '';
            $header = 'UN Habitat';
            $message.= 'Dear '.ucfirst($fname).'&nbsp;'.ucfirst($lname).',<br/><br/>';
            $message.= 'You have issues to clear.<br/>';
            $message.= '<br/><br/>Thank you ,<br/>';
            $subject = 'Issue Publications';
 ///////////           $this->my_library->send_email($email_to, $subject, $message, $header, $email_from);
            return true;
    /******************************************************/
        }
        function update_borrower_quantity_status($borrower_qty_id){
            $update['status'] = 1;
            $this->general_db_model->update('tbl_borrower_quantities', $update, 'id = '.$borrower_qty_id);
        }
}
?>